﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Net.Fan.SmallPig.DataAccess;
using System.Data;

public partial class User_Departments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = DeptDA.GetDepts();
        this.ListBox1.DataSource = ds.Tables[0];
        this.DataBind();
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        string name = this.TBName.Text.Trim();
        string pID = this.TBParentID.Text.Trim();
        string despt = this.TBDespt.Text.Trim();
        int i;
        if (pID!="")
        {
            try
            {
                  i= int.Parse(pID);
                if (i>=0)
	{
		 DeptDA.InsertDept(name, despt, i);
	}
                  
            }
            catch (Exception)
            {
                Response.Write("<script>alert('您填写的上级ID 不是数字 或是 数字太大 请重新填写')</script>");
                return;
                //throw;
            }
        }
        else
        {
            DeptDA.InsertDept(name, despt, 0);
        }

        
        

    }
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string name = this.TBName.Text.Trim();
        string pID = this.TBParentID.Text.Trim();
        string despt = this.TBDespt.Text.Trim();
        int i=0;
        if (pID != "")
        {
            try
            {
                i = int.Parse(pID);
                if (i >= 0)
                {
                    DeptDA.UpdateDept(int.Parse(this.ListBox1.SelectedValue), name, despt, i);
                }

            }
            catch (Exception)
            {
                Response.Write("<script>alert('您填写的上级ID 不是数字 或是 数字太大 请重新填写')</script>");
                return;
                //throw;
            }
        }
        DeptDA.UpdateDept(int.Parse(this.ListBox1.SelectedValue),name,despt,i);
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
